package com.config;/*
 *
 *作者：张继成
 *任务：
 *
 *备注：
 *@Date2023/06/04/11:35
 ***/

import com.interceptor.AuthorizationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
public class InterceptorConfig extends WebMvcConfigurationSupport {

    @Bean
    public AuthorizationInterceptor getAuthorizationInterceptor() {
        return new AuthorizationInterceptor();
    }
    //资源拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(getAuthorizationInterceptor())
                //此处是拦截路径以及资源放行
                        .addPathPatterns("/**")
                //此处是我放行资源，放行资源需要使用项目下的第一级路径，后期上线打包
                        .excludePathPatterns("/static/**")
                //Swagger接口放行资源
                        .excludePathPatterns("/swagger-resources/**/", "/webjars/**", "/v2/**", "/swagger-ui.html/**","doc.html","/error")
                //xueshengsheng/sendMsg
                        .excludePathPatterns("/xuesheng/sendMsg/**/")
                //支付接口测试
                        .excludePathPatterns("/alipay/**")
                //内网穿透
                        .excludePathPatterns("/http://qwbswt.natappfree.cc/**")
        ;

        super.addInterceptors(registry);
    }

    //springboot 2.0配置WebMvcConfigurationSupport之后，会导致默认配置被覆盖，要访问静态资源需要重写addResourceHandlers方法
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/resources/")
                .addResourceLocations("classpath:/static/")
                .addResourceLocations("classpath:/admin/")
                .addResourceLocations("classpath:/front/")
                .addResourceLocations("classpath:/public/")

        ;
        // 解决swagger无法访问
        registry.addResourceHandler("/swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        // 解决swagger的js文件无法访问
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        //解决Swagger接口的doc页面
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        super.addResourceHandlers(registry);
    }




}
